package com.example.user_manage.controller;

import com.example.user_manage.enums.REnum;
import com.example.user_manage.result.R;
import com.example.user_manage.service.PhoneVerificationService;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 手机验证控制器
 */
@AllArgsConstructor
@Validated
@Controller
public class PhoneVerificationController {
    /**
     * 手机验证服务对象
     */
    private final PhoneVerificationService phoneVerificationService;

    @PostMapping("/sendVerificationCode")
    public @ResponseBody R<Void> sendVerificationCode(
            @RequestParam @NotBlank(message = "电话号不能为空") String phoneNumber) {

        //发送验证码短信并判断是否成功
        if(phoneVerificationService.sendVerificationCode(phoneNumber)) {
            //验证码短信发送成功
            return R.ok(REnum.SUCCESS_VERIFICATION_PHONE);
        }else {
            //验证码短信发送失败
            return R.fail(REnum.FAIL_VERIFICATION_PHONE.getCode(),
                    REnum.FAIL_VERIFICATION_PHONE.getMessage());
        }
    }

    /**
     * 进行验证
     * @param phoneNumber 手机号
     * @param code 验证码
     * @return 结果封装对象
     */
    @PostMapping("/toVerification")
    public @ResponseBody R<Void> toVerification(
            @RequestParam @NotBlank(message = "电话号码不能为空") String phoneNumber
            , @RequestParam @NotBlank(message = "验证码不能为空") String code) {
        //调用手机验证服务对象的验证方法
        phoneVerificationService.toVerification(phoneNumber, code);
        //验证成功
        return R.ok(REnum.SUCCESS_VERIFICATION_CODE);
    }
}
